package com.cancan.demo.service;

import org.bytedeco.javacv.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class RTSPFrameCapture {
    public static void main(String[] args) {
        String rtspUrl = "rtsp://admin:cz12345678@192.168.101.73"; // 替换为你的RTSP流地址
        String outputImagePath = "D:\\test\\captured_frame.jpg"; // 保存的图片路径

        try (FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(rtspUrl);
             Java2DFrameConverter converter = new Java2DFrameConverter()) {

            grabber.setOption("rtsp_transport", "tcp"); // 使用TCP传输协议
            grabber.start();

            Frame frame = grabber.grabImage(); // 抓取一帧
            if (frame != null) {
                BufferedImage bufferedImage = converter.getBufferedImage(frame);
                ImageIO.write(bufferedImage, "jpg", new File(outputImagePath));
                System.out.println("帧已保存为 " + outputImagePath);
            } else {
                System.out.println("无法抓取帧");
            }

            grabber.stop();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}